package com.njlime.sys.service;

import com.njlime.core.helper.GlobalValue;
import com.njlime.sys.entities.UserConfig;
import com.njlime.sys.repository.UserConfigDao;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by Dean on 2017-7-19.
 */
@Service
@Transactional(readOnly = true)
public class UserConfigService {

    @Resource
    private UserConfigDao userConfigDao;


    /**
     * 获取单位列表
     * @param unitId
     * @return
     */
    public   List<Map<String,Object>>  list(int  unitId){
        return userConfigDao.list(unitId);
    }

    /**
     * 修改状态
     *
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    public int updateStatus(int id) {
        return userConfigDao.updateStatus(id);
    }

    /**
     * 定义列表
     * @return
     */
    public List<Map<String,Object>> defineList(){
        return userConfigDao.defineList();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Transactional
    public int del(int id){
        return userConfigDao.del(id);
    }

    /**
     * 创建默认设置
     * @param unit_id
     */
    @Transactional
    public void createDefault(int unit_id){
        List<Map<String,Object>> list = defineList();
        if(list != null && list.size() > 0){
            for(Map<String,Object> map : list){
                UserConfig userConfig = new UserConfig();
                userConfig.setDefine_id(Integer.valueOf(map.get("id").toString()));
                userConfig.setDel(GlobalValue.NO);
                userConfig.setStatus(1);
                userConfig.setUnit_id(unit_id);
                userConfigDao.save(userConfig);
            }
        }
    }

}
